<?php


namespace App\Http\Repositories\Admin\Member;

use App\Exceptions\ApiException;
use App\Models\Admin\Models\MemberModel;
use App\Http\Repositories\BaseRepository as Repository;

class MemberRepository extends  Repository
{
    private $member;
    public function __construct(MemberModel $member)
    {
        $this->member = $member;
    }

    /**
     * 获取列表
     * @param $data
     * @return mixed
     */
    public function getMemberList($data)
    {
        $where = $this->handleWhere($data);

        return $this->member->getMemberList($where);
    }

    /**
     * where条件处理
     * @param $data
     * @return array
     */
    public function handleWhere($data)
    {
        $where = [];

        is_real_exists(@$data['member_name']) && $where['member_name'] = ['like', '%'.$data['member_name'].'%'];
        is_real_exists(@$data['email']) && $where['email'] = ['like', '%'.$data['email'].'%'];
        is_real_exists(@$data['mobile']) && $where['mobile'] = ['like', '%'.$data['mobile'].'%'];
        is_real_exists(@$data['create_time']) && $where['create_time'] = ['between', $data['create_time']];
        is_real_exists(@$data['update_time']) && $where['update_time'] = ['between', $data['update_time']];

        return $where;
    }

    /**
     * 获取详情
     * @param $data
     * @return mixed
     * @throws ApiException
     */
    public function getMemberInfo($data)
    {
        $where = [];
        is_real_exists(@$data['id']) && $where['id'] = $data['id'];
        is_real_exists(@$data['uuid']) && $where['uuid'] = $data['uuid'] ;
        is_real_exists(@$data['token']) && $where['token'] = $data['token'] ;

        return $this->member->getFirst($where);
    }

    /**
     * 添加
     * @param $data
     * @return mixed
     */
    public function addMember($data)
    {
        $data['uuid'] = md5(uniqid($data['member_name'].$data['password'].microtime()));
        $data['password'] = md5($data['password'].$data['member_name']);

        return $this->member->addItem($data);
    }

    /**
     * 删除
     * @param $data
     * @return array
     */
    public function delMember($data)
    {
        return $this->member->deleteItem($data);
    }

    /**
     * 编辑
     * @param $data
     * @return array
     */
    public function editMember($data)
    {
        $where['id'] = $data['id'];

        is_real_exists(@$data['password']) && $data['password'] = md5($data['password']);

        return $this->member->editItem($where, $data);
    }


    public function login($data)
    {
        $where = [];
        is_real_exists(@$data['member_name']) && $where['member_name'] = $data['member_name'];
        is_real_exists(@$data['password']) && $where['password'] = md5($data['password']);

        return $this->member->login($where);
    }

    public function loginOut($data)
    {
        return [];
    }
}
